package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.action.FiAmortResVo;
import com.miplus.ccrm.core.controller.vo.action.FiReliefResVo;
import com.miplus.ccrm.core.controller.vo.credit.FiTaskReqVo;
import com.miplus.ccrm.core.controller.vo.credit.FiTaskResVo;
import com.miplus.ccrm.core.controller.vo.credit.HistTaskReqVo;
import com.miplus.ccrm.core.controller.vo.credit.HistTaskResVo;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.FiTaskOldhistMapper;
import com.miplus.ccrm.core.service.ExportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;

/**
* @author pc
* @description 针对表【fi_task_oldhist(系统上线前的催记历史)】的数据库操作Service实现
* @createDate 2024-04-23 14:45:10
*/
@Service
public class FiTaskOldhistServiceImpl extends MPJBaseServiceImpl<FiTaskOldhistMapper, FiTaskOldhist>
    implements FiTaskOldhistService{

    @Resource
    private ExportService exportService;
    @Resource
    private FiImportHistService fiImportHistService;

    @Override
    public IPage<HistTaskResVo> viewHistTask(HistTaskReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                HistTaskResVo.class,
                new MPJLambdaWrapper<FiTaskOldhist>()
                        .selectAs("DATE_FORMAT(t.dodt,'%Y-%m-%d %H-%i-%s')",FiTaskOldhist::getDodt)
                        .selectAs("DATE_FORMAT(t.updt,'%Y-%m-%d %H-%i-%s')",FiTaskOldhist::getUpdt)
                        .selectAll(FiTaskOldhist.class)
                        .eq(JPubUtils.IsNotEmpty(paras.getDatabatch()),FiTaskOldhist::getDatabatch,paras.getDatabatch())
                        .eq(JPubUtils.IsNotEmpty(paras.getCardnumber()),FiTaskOldhist::getCardnumber,paras.getCardnumber())
                        .eq(JPubUtils.IsNotEmpty(paras.getPhone()),FiTaskOldhist::getPhone,paras.getPhone())
                        .eq(JPubUtils.IsNotEmpty(paras.getIdnumber()),FiTaskOldhist::getIdnumber,paras.getIdnumber())
                        .eq(JPubUtils.IsNotEmpty(paras.getBatchid()),FiTaskOldhist::getBatchid,paras.getBatchid())
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), FiTaskOldhist::getUpdt,JPubUtils.StringToDate(paras.getStartdt()),JPubUtils.StringToDate(paras.getEnddt()))
                        .between(JPubUtils.IsNotEmpty(paras.getStartdodt()), FiTaskOldhist::getDodt,JPubUtils.StringToDate(paras.getStartdodt()),JPubUtils.StringToDate(paras.getEnddodt()))
                        .eq(JPubUtils.IsNotEmpty(paras.getCustname()),FiTaskOldhist::getCustname,paras.getCustname())
                        .in(JPubUtils.IsNotEmpty(paras.getIdnumberlist()), FiTaskOldhist::getIdnumber, paras.getIdnumberlist())
                        .in(JPubUtils.IsNotEmpty(paras.getCardnolist()), FiTaskOldhist::getCardnumber, paras.getCardnolist())
                        .orderByAsc(FiTaskOldhist::getDodt)
        );
    }

    @Override
    public CmdResVo expHistTask(String $usrid, HistTaskReqVo paras, HttpServletResponse response)
    {
        IPage<HistTaskResVo> pages=viewHistTask(paras);
        ArrayList arr=new ArrayList();

        String exptempid=JPubUtils.getExpTempID(paras.getExpfilename(),"历史催记列表");

        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                $usrid
        );

        int okcount=resVo.getCount();
        if(okcount>0){
            fiImportHistService.AddExportHist(
                    resVo.getKeyId(),
                    $usrid,
                    exptempid,
                    filename,
                    okcount
            );
        }

        return resVo;
    }
}




